<?php
// 微信端 客户
namespace H5\Controller;
use Think\Controller;
class ClientController extends Controller {
	// 列表页
    public function index(){
        $userid = session('userid');
        $level  = session('level');
        if($level == 1) {  // 普通销售员身份
            $map['uid'] = $userid;
        }
        
    	$clientList = M('client')->where($map)->field('id,uid,client_name,location,add_time')->order('id desc')->select();
    	foreach($clientList as $k => $val){
    		$clientList[$k]['add_time'] = date('Y-m-d H:i',$val['add_time']);
    	}
    	//p($clientList);die;

    	$this->assign('clientList',$clientList);
    	$this->assign('menu','client'); // 客户
        $this->display();
    }

	// 客户详情页
    public function detail(){
    	$id = I('get.id');
    	$map['id'] = $id;
    	$clientArr = M('client')->where($map)->find();
        $clientArr['shop_birthday'] = $clientArr['shop_birthday'] ? date('Y-m-d',$clientArr['shop_birthday']) : '';
    	$data['cid'] = $id;
    	$contactList = M('contact')->where($data)->field('id,realname')->select();

    	$this->assign('clientArr',$clientArr);
    	$this->assign('contactList',$contactList);
    	$this->assign('menu','client'); // 客户
        $this->display();
    }

	// 负责人详情页
    public function contactDetail(){
    	$id = I('get.id');
    	$map['id'] = $id;
    	$contactArr = M('contact')->where($map)->find();
    	$contactArr['birthday'] = $contactArr['birthday'] ? date('Y-m-d',$contactArr['birthday']) : '';
    	//p($contactArr);die;
    	$this->assign('contactArr',$contactArr);
    	$this->assign('menu','client'); // 客户
    	$this->display();

    }

    // 添加客户页面
    public function add(){
        $id = I('get.cid');  // 添加负责人 添加成功后 传此客户id
        if($id){ // 有则表示此用户已添加负责人，即此客户记录已添加到数据库
            $map['id'] = $id;
            $clientArr = M('client')->where($map)->find();
            $clientArr['shop_birthday'] = $clientArr['shop_birthday'] ? date('Y-m-d',$clientArr['shop_birthday']) : '';
            $data['cid'] = $id;
            $contactList = M('contact')->where($data)->field('id,realname')->select();
            $this->assign('clientArr',$clientArr);
            $this->assign('contactList',$contactList);
            $this->assign('id',$id);
        }
    	$this->assign('menu','client'); // 客户
    	$this->display();

    }

    // 添加客户 提交  
    public function addCheck(){
        $id = I('get.id');  // 由添加负责人按钮 触发 ajax get传值过来
        if(!$id){  // 如果没有  两种情况：1、说明未添加负责人，直接添加客户；2、第一次添加负责人
            $client_name       = I('post.client_name');
            $shop_manager      = I('post.shop_manager');
            $shop_birthday     = I('post.shop_birthday');
            $visit_year_num    = I('post.visit_year_num');
            $visit_quarter_num = I('post.visit_quarter_num');
            $visit_month_num   = I('post.visit_month_num');
            $remark            = I('post.remark');
            $location          = I('post.location');
            $longitude         = I('post.longitude');
            $latitude          = I('post.latitude');
            $tel               = I('post.tel');
            $qq                = I('post.qq');
            $wechat            = I('post.wechat');
            $email             = I('post.email');

            $data['uid'] = session('userid');
            $data['client_name']       = $client_name;
            $data['shop_manager']      = $shop_manager;
            $data['shop_birthday']     = $shop_birthday ? strtotime($shop_birthday) : '';
            $data['visit_year_num']    = $visit_year_num;
            $data['visit_quarter_num'] = $visit_quarter_num;
            $data['visit_month_num']   = $visit_month_num;
            $data['remark']            = $remark;
            $data['location']          = $location;
            $data['longitude']         = $longitude;
            $data['latitude']          = $latitude;
            $data['tel']               = $tel;
            $data['qq']                = $qq;
            $data['wechat']            = $wechat;
            $data['email']             = $email;
            $data['add_time']          = time();

            $rel = M('client')->add($data);
            if($rel){
                $msg = array('state'=>200,'id'=>$rel,'content'=>'添加成功');
            }else{
                $msg = array('state'=>500,'content'=>'添加失败');
            }
        }else{  // 2种情况：1、再次添加负责人； 2、提交客户信息，保存
            $client_name       = I('post.client_name');
            $shop_manager      = I('post.shop_manager');
            $shop_birthday     = I('post.shop_birthday');
            $visit_year_num    = I('post.visit_year_num');
            $visit_quarter_num = I('post.visit_quarter_num');
            $visit_month_num   = I('post.visit_month_num');
            $remark            = I('post.remark');
            $location          = I('post.location');
            $longitude         = I('post.longitude');
            $latitude          = I('post.latitude');
            $tel               = I('post.tel');
            $qq                = I('post.qq');
            $wechat            = I('post.wechat');
            $email             = I('post.email');

            $data['id']  = $id;
            $data['client_name']       = $client_name;
            $data['shop_manager']      = $shop_manager;
            $data['shop_birthday']     = $shop_birthday ? strtotime($shop_birthday) : '';
            $data['visit_year_num']    = $visit_year_num;
            $data['visit_quarter_num'] = $visit_quarter_num;
            $data['visit_month_num']   = $visit_month_num;
            $data['remark']            = $remark;
            $data['location']          = $location;
            $data['longitude']         = $longitude;
            $data['latitude']          = $latitude;
            $data['tel']               = $tel;
            $data['qq']                = $qq;
            $data['wechat']            = $wechat;
            $data['email']             = $email;
            $data['add_time']          = time();

            $rel = M('client')->save($data);

            if($rel){
                $msg = array('state'=>200,'id'=>$id,'content'=>'添加成功');
            }else{
                $msg = array('state'=>500,'id'=>$id,'content'=>'添加失败');
            }
            
        }
        
        $this->ajaxReturn($msg);

    }

    // 添加负责人页面
    public function contactAdd(){
        $cid = I('get.cid');  // 已添加的客户id
        $this->assign('cid',$cid);
    	$this->assign('menu','client'); // 客户
    	$this->display();

    }

    // 添加负责人 提交
    public function contactAddCheck(){
    	$cid = I('get.cid');  // 已添加的客户id

        $realname = I('post.realname');
        $birthday = I('post.birthday');
        $sex      = I('post.sex');
        $tel      = I('post.tel');
        $qq       = I('post.qq');
        $wechat   = I('post.wechat');
        $email    = I('post.email');

        $data['uid']      = 1;  // 添加人id 用户id
        $data['cid']      = $cid;
        $data['realname'] = $realname;
        $data['birthday'] = $birthday ? strtotime($birthday) : '';
        $data['sex']      = $sex;
        $data['tel']      = $tel;
        $data['qq']       = $qq;
        $data['wechat']   = $wechat;
        $data['email']    = $email;
        $data['add_time'] = time();

        $rel = M('contact')->add($data);

        if($rel){
            $msg = array('state'=>200,'content'=>'添加成功');
        }else{
            $msg = array('state'=>500,'content'=>'添加失败');
        }

        $this->ajaxReturn($msg);

    }


    public function del(){
        $id = I('get.id');
        $map1['id'] = $id;
        $map2['cid'] = $id;
        $rel1 = M('client')->where($map1)->delete();
        $rel2 = M('contact')->where($map2)->delete();
        $this->redirect('Client/index');
    }
}